package org.semanticflash.rdf.utils
{
	
	import com.adobe.net.URI;
	
	import mx.utils.UIDUtil;
	
	import org.semanticflash.rdf.values.IRI;
	import org.semanticflash.rdf.vocab.Vrdf;


	
	/**
	 * 
	 * Static utility methods specific to the rdf domain
	 * 
	 * 
	 * @author aldo.bucchi@gmail.com
	 * 
	 */	
	public class RDFUtil
	{
		
		

		/**
		 * Tim Berners-Lee. Commonly used for testing purposes
		 */		
		public static const TIMBL			:IRI = IRI.instance( "http://www.w3.org/People/Berners-Lee/card#i" );
		
		/**
		 * Aldo Bucchi. Commonly used for testing purposes
		 */		
		public static const ALDO_BUCCHI	:IRI = IRI.instance( "http://www.univrz.com/dataspace/person/aldo.bucchi#this" );		
		
		
		
		
		/**
		 * 
		 * returns an ordinal predicate URI
		 * 
		 * @param index
		 * @return 
		 * 
		 */
		public static function getOrdinalPropertyURI( index:uint ):String
		{
			return Vrdf.ns.uri + "_" + index;
		}
		
		
		/**
		 * 
		 * returns the index if it is an ordinal property
		 * otherwise returns -1
		 * 
		 * @param uri
		 * @return 
		 * 
		 */		
		public static function parseAsOrdinalProperty( uri:String ):int
		{
			var s:String = uri;
			
			if ( s.indexOf( Vrdf.ns.uri ) != 0 )
				return -1; // not in the rdf namespace
			
			
			var localPart:String = s.substr( Vrdf.ns.uri.length );
			
			if ( localPart.charAt( 0 ) != "_" )
				return -1; // does not have the trailing underscore
			
			var ordinalPart:String = localPart.substr(1);
			
			// check if it is composed exclusively of ints
			// TODO: some regex here maybe? for now anything will do
			return ordinalPart as int;
		}
		
		
		
		
		/**
		 * 
		 * Will create a unique local uri String in the form
		 * _:65DF4DF56...
		 * 
		 * Internally, it uses UIDUtil.createUID() so the URI should be unique
		 * to "this" running instance of the Flash Player.
		 * 
		 * This is conformant to the Blank Node semantics, which state that a Blank Node is only
		 * valid in a restricted, non universal scope ( usually a Graph ).
		 * 
		 * @return 
		 * 
		 */		
		public static function getUniqueLocalBNodeURIString():String
		{
			return "_:" + UIDUtil.createUID();		
		}		
		
				
	
		
		
		

		/**
		 * 
		 * A special iri comparison function where iris are compared textually.
		 * <pre>
		 * "urn:char:b" is considered bigger than "urn:char:a".
		 * </pre> 
		 * 
		 * By using this compare function you can predict which IRI is the preferred
		 * ( and ultimately, cannonical ) one when equating.
		 * Useful for building automated tests.
		 * 
		 * The default internal compare function is based on sequential IDs, which is faster but
		 * unpredictable.
		 * 
		 * @param iri1
		 * @param iri2
		 * @return 
		 * 
		 */
		public static function compareIRIsBasedOnTheirStringForm( iri1:IRI, iri2:IRI ):int
		{
			
			if ( iri1 === iri2 )
				return 0;
			
			if ( iri1.stringForm > iri2.stringForm )
				return 1;
			
			return -1;
			
		}		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		/**
		 * 
		 * 
		 * @param stringForm
		 * @return an array of length 1 ( if no namespace was detected ) or length 2 of the form [ ns, localName ]
		 * 
		 */
		public static function splitNSAndLocal( stringForm:String ):Array
		{
			var uri:URI = new URI( stringForm );
			
			if( !uri )
				return [ stringForm ];
			
			if( uri.scheme == "https" || uri.scheme == "http" )
			{

				for( var index:int = stringForm.length ; index > uri.scheme.length + 3; index-- )
				{
					if( stringForm.charAt( index ) == '#' || stringForm.charAt( index ) == ':' || stringForm.charAt( index ) == '/' )
					{
						
						if ( index == stringForm.length - 1 ) // charactef found at the end of string. there is no localName left to the right
							break;
						
						return [ stringForm.substr( 0, index + 1 ), stringForm.substr( index + 1 ) ];
					}
				}

			}
			
			return [ stringForm ];
		}
		
		
		
		
		
		
		
		
		
	}

}